import { loginApi, userInfoApi } from '@/api/user'
import router from '@/router'
import { getToken, setToken } from '@/utils/auth'
import { Message } from 'element-ui'

const state = {
  // 定义token
  token: getToken(),
  // 用户信息
  userInfo: {}
}

const mutations = {
  loginToken(state, payload) {
    state.token = payload
  },
  navbarUser(state, payload) {
    state.userInfo = payload
  }
}
const actions = {
  async LoginTokens({ commit }, payload) {
    const res = await loginApi(payload)
    // console.log(res.data.token)
    commit('loginToken', res.data.token)
    // 存本地
    setToken(res.data.token)
    Message.success('登录成功')
    // 跳转页面
    router.push('/')
  },
  // 发送用户信息请求
  async navbarUsers({ commit }) {
    const res = await userInfoApi()
    commit('navbarUser', res.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
